package com.eros.wx.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;

import com.eros.framework.proxy.SplashActivityProxy;
import com.eros.wx.R;

/**
 * Created by Carry on 2017/8/23.
 */

public class SplashActivity extends Activity {
    private SplashActivityProxy activityProxy;

    /**
     * 判断是否需要检测，防止不停的弹框
     */
    private boolean isNeedCheck = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        activityProxy = new SplashActivityProxy();
//        activityProxy.onCreateInit(this);
        checkPermission();
        if(!isNeedCheck){
            activityProxy.onCreateInit(this);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        activityProxy.onStart(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        activityProxy.onResume(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        activityProxy.onRestart(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        activityProxy.onPause(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        activityProxy.onStop(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        activityProxy.onDestroy(this);
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[]
        grantResults) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && requestCode == 200) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//                initFragment();
                activityProxy.onCreateInit(this);
            } else {
                Toast.makeText(this, "请在设置中打开权限后继续", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                Uri uri = Uri.fromParts("package", getPackageName(), null);
                intent.setData(uri);
                activityProxy.onCreateInit(this);
                startActivityForResult(intent, 200);
            }
        }
    }

    private void checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
            int i = ContextCompat.checkSelfPermission(this, permissions[0]);
            if (i != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, permissions, 200);
                return;
            }else{
                isNeedCheck = false;
            }
        }else{
            isNeedCheck = false;
        }
    }

}
